package cn.xpg.controller;

import java.io.IOException;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.List;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import cn.xpg.entity.Sale;
import cn.xpg.service.SaleService;
import cn.xpg.tools.ExportExcelUtil;
import cn.xpg.tools.PageSupport;

@Controller
public class SaleController {

	@Resource(name = "saleService")
	private SaleService saleService;

	// 分页查询销售单信息
	@RequestMapping("/getSaleList.do")
	public String getSaleList(HttpServletRequest request) {
		String sale_id = request.getParameter("sale_id");
		String pageIndex = request.getParameter("pageIndex");
		int pageSize = 3;
		int currentPageNo = 1;
		if (pageIndex != null) {
			try {
				currentPageNo = Integer.valueOf(pageIndex);
			} catch (Exception e) {
				// TODO: handle exception
				e.printStackTrace();
			}
		}
		int totalCount = 0;
		totalCount = saleService.getSaleCount(sale_id);
		System.out.println("查询销售单信息！！！！！！！！！！————>getSaleList");

		PageSupport pages = new PageSupport();
		pages.setCurrentPageNo(currentPageNo);
		pages.setPageSize(pageSize);
		pages.setTotalCount(totalCount);

		int totalPageCount = pages.getTotalPageCount();

		if (currentPageNo < 1) {
			currentPageNo = 1;
		} else if (currentPageNo > totalPageCount) {
			currentPageNo = totalPageCount;
		}

		List<Sale> saleList = null;
		saleList = saleService.getSaleList(sale_id, (currentPageNo - 1) * pageSize, pageSize);

		request.setAttribute("saleList", saleList);
		request.setAttribute("totalPageCount", totalPageCount);
		request.setAttribute("pages", pages);
		request.setAttribute("sale_id", sale_id);
		return "/jsp/jinxiaocun/add_sales";
	}

	// 导入excel
	@RequestMapping("/getSaleListTOExcel.do")
	public void getSaleListTOExcel(HttpServletRequest request, HttpServletResponse response) throws Exception {
		String sale_id = request.getParameter("sale_id");
		if (sale_id == null)
			sale_id = "";
		List<Sale> saleList = saleService.getSaleListTOExcel(sale_id);
		ExportExcelUtil ex = new ExportExcelUtil();
		String title = "Warehouse receipt";
		String[] headers = { "销售单编号", "下单时间", "销售时间", "数量", "进价", "售价", "数量", "客户名称", "客户电话" };
		List<String[]> dataset = new ArrayList<String[]>();
		for (int i = 0; i < saleList.size(); i++) {
			Sale sale = saleList.get(i);
			dataset.add(new String[] { String.valueOf(sale.getSale_id()), String.valueOf(sale.getAddsale_date()),
					String.valueOf(sale.getSale_date()), String.valueOf(sale.getProduct_num()),
					String.valueOf(sale.getSale_intoprice()), String.valueOf(sale.getSale_outprice()),
					sale.getSale_note(), sale.getCustomer_name(), sale.getCustomer_telephone() });
		}
		/*
		 * OutputStream out = null; try { out = new FileOutputStream("C://output.xls");
		 * ex.exportExcel(title,headers, dataset, out); out.close(); } catch (Exception
		 * e) { e.printStackTrace(); }
		 */
		OutputStream out = null;// 创建一个输出流对象
		try {
			out = response.getOutputStream();//
			response.setHeader("Content-disposition", "attachment; filename=" + "图书信息记录.xls");// filename是下载的xls的名，建议最好用英文
			response.setContentType("application/msexcel;charset=UTF-8");// 设置类型
			response.setHeader("Pragma", "No-cache");// 设置头
			response.setHeader("Cache-Control", "no-cache");// 设置头
			response.setDateHeader("Expires", 0);// 设置日期头
			String rootPath = request.getSession().getServletContext().getRealPath("/");
			ex.exportExcel(rootPath, title, headers, dataset, out);
			out.flush();
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			try {
				if (out != null) {
					out.close();
				}
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}

	// 新增销售单
	@RequestMapping("addSale.do")
	public String addSale(@Valid Sale sale) {
		System.out.println("新增销售单信息！！！！！！！！！！————>addSale");
		if (saleService.addSale(sale) > 0) {
			return "redirect:/getSaleList.do";
		} else {
			return "/jsp/user/manage_qy";
		}
	}

	// 获取销售单Id
	@RequestMapping("getSaleId.do")
	public String getSaleId(HttpServletRequest request) {
		System.out.println("获取销售单Id！！！！！！！！！！————>getSaleId");
		int Sale_id = Integer.parseInt(request.getParameter("Sale_id"));
		Sale Sale = saleService.getSaleId(Sale_id);
		request.setAttribute("Sale", Sale);
		return "/jsp/user/manage_qy_update";
	}

	// 修改销售单
	@RequestMapping("updateSale.do")
	public String updateSale(@Valid Sale Sale, HttpServletRequest request) {
		System.out.println("修改销售单信息！！！！！！！！！！————>updateSale");
		if (saleService.updateSale(Sale) > 0) {
			System.out.println("修改成功！！！！！！");
			return "redirect:/getSaleList.do";
		} else {
			System.out.println("修改失败！！！！！！");
			request.setAttribute("Sale", Sale);
			return "/jsp/user/manage_qy_update";
		}
	}

	// 删除销售单
	@RequestMapping("deleteSale.do")
	public String deleteSale(HttpServletRequest request) {
		System.out.println("删除销售单信息！！！！！！！！！！————>deleteSale");
		int Sale_id = Integer.valueOf(request.getParameter("sale_id"));
		if (saleService.deleteSale(Sale_id) > 0) {
			System.out.println("删除成功！！！！！！");
		} else {
			System.out.println("删除失败！！！！！！");
		}
		return "redirect:/getSaleList.do";
	}
}
